from typing import Sequence

# Django
from django.contrib import admin
from django.contrib.admin.sites import AdminSite
from django.http.request import HttpRequest
from django.db.models.fields.related import ForeignKey

from import_export import resources
from import_export.admin import ImportExportModelAdmin

child_model = None


class CommonAdmin(object):

    def __init__(self, base_model, list_display=None, add_fields: list = None, modify_fields: list = None,
                 list_filter: list = None):
        self.base_model = base_model
        child_model = base_model

        class AdminResource(resources.ModelResource):
            class Meta:
                model = self.base_model

            def get_import_fields(self):
                import_fields = []
                for field_name in self.get_import_order():
                    if field_name in self.fields:
                        import_fields.append(self.fields[field_name])
                        continue
                    # issue 1815
                    # allow for fields to be referenced by column_name in `fields` list
                    for field in self.fields.values():
                        if field.column_name == field_name:
                            import_fields.append(field)
                            continue
                return import_fields

            def get_export_headers(self, selected_fields=None):
                """
                实现导出verbose_name
                Args:
                    selected_fields:

                Returns:

                """
                if selected_fields is not None:
                    fields = []
                    for data in selected_fields:
                        field = child_model._meta.get_field(data)
                        verbose_name = field.verbose_name
                        fields.append(verbose_name)
                    return fields
                return super(AdminResource,self).get_export_headers(selected_fields)


        class CommonImportExportAdmin(ImportExportModelAdmin):
            resource_class = AdminResource

        @admin.register(self.base_model)
        class DefaultAdmin(CommonImportExportAdmin):
            def __init__(self, model: type, admin_site: AdminSite) -> None:
                super().__init__(model, admin_site)
                # 每页展示多少条数据
                self.list_per_page = 20

            def get_list_display(self, request: HttpRequest) -> Sequence[str]:
                if list_display is not None:
                    return list_display
                temp_list = []
                for filed in base_model._meta.fields:
                    temp_list.append(filed.name)
                tuple_list = tuple(temp_list)
                return tuple_list

            def get_list_filter(self, request: HttpRequest) -> Sequence[str]:
                if list_filter is not None:
                    return list_filter
                return super().get_list_filter(request)

            # def get_fields(self, request: HttpRequest, obj: Any | None = ...) -> Sequence[Callable[..., Any] | str]:
            #     if obj and modify_fields:
            #         return modify_fields
            #     if (not obj ) and add_fields:
            #         return add_fields
            #     return super().get_fields(request, obj)

            def get_search_fields(self, request):
                temp_list = []
                for filed in base_model._meta.fields:
                    if type(filed) == ForeignKey:
                        continue
                    temp_list.append(filed.name)
                return temp_list
